#coding=utf-8

from django.db import models
from django.contrib.auth.models import User

from question.models import Category


class StarManager(models.Manager):
    def add(self, vote):
        star,created = self.get_or_create(user=vote.user, category=vote.question.category)
        if not created:
            category_star.count += 1
            category_star.save()
    
    def get_count(self, user, category):
        try:
            star = self.get(user=vote.user, category=category)
            return star.count
        except Star.DoesNotExist:
            return 0


class Star(models.Model):
    user = models.ForeignKey(User)
    category = models.ForeignKey(Category)
    count = models.IntegerField(default=1)
    created_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    updated_time = models.DateTimeField(auto_now=True)
    
    objects = StarManager()
    
    class Meta:
        verbose_name = u'加星'
        verbose_name_plural = u'加星列表'
        db_table = "t_star"
        unique_together = ("user", "category")  # 两个人之间只能保证一种关系的存在
        ordering = ["-created_time"]
    
